<?php

class CommentDao{

    protected $table = 'comment';

    public function insertObject($obj){
        
            $result = DB::insert(DB::raw("INSERT INTO comment (issue_id, user_id, comment_detail) ".
                    "VALUES ('".$obj->getIssue()->getId()."' ".
                            ", '".$obj->getEmployee()->getId()."' ".
                            ", '".$obj->getCommentDetail()."' ".
                            ")"));
            if (!$result) { return null; }
            return DB::getPdo()->lastInsertId();
    }
    

    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM comment"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
        return $list;
    }

    
    public function getListByIssueId($taskId){
        $query = "SELECT * FROM comment ";
        if($taskId!=null && $taskId!="")
            $query .= "where issue_id='".$taskId."'";
        $result = DB::select(DB::raw($query));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);            
        }
        
        return $list;
    }
    
    public function toObject($rowdata=NULL){
    
        $obj = new Comment();
        $obj->setId($rowdata->id);
        $obj->setIssue($rowdata->issue_id);
            $user = new User();
            $user->setId($rowdata->user_id);
            $user->setIsLoaded(false);
            $obj->setEmployee($user);
            
        $obj->setCommentDetail($rowdata->comment_detail);
        $obj->setCreatedDate($rowdata->created_date);
        $obj->IsLoaded(true);
        
        return $obj;
        
    }





}